package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.company.EntityDockServer;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.company.dao.IDaoDockServer;
import com.ocom.company.service.DockServerService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DockServerServiceImpl extends ServiceImpl<IDaoDockServer, EntityDockServer> implements DockServerService {


    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public EntityDockServer getDockServerInfo(String dockCode, String svrAppid, String serverCode, String serverName) {
        QueryWrapper<EntityDockServer> queryWrapper = Wrappers.query();
        if (StringUtils.isNotBlank(dockCode)) {
            queryWrapper.eq("dock_code", dockCode);
        }
        if (StringUtils.isNotBlank(svrAppid)) {
            queryWrapper.eq("svr_appid", svrAppid);
        }
        if (StringUtils.isNotBlank(serverCode)) {
            queryWrapper.eq("server_code", serverCode);
        }
        if (StringUtils.isNotBlank(serverName)) {
            queryWrapper.eq("server_name", serverName);
        }
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Map<String, Object> getDockServerList(String dockCode, Map<String,Object> parameters, Integer page, Integer size) {
        QueryWrapper<EntityDockServer> queryWrapper = Wrappers.query();
        queryWrapper.eq("dock_code", dockCode);
        parameters.forEach((key,value) -> {
            queryWrapper.like(key, value);
        });
        Integer total = this.count(queryWrapper);

        if (null == page || null == size || (page < 0 && size < 0)) {
            page = 0;
            size = 500;
        }
        PageHelper.startPage(page, size);
        List<EntityDockServer> list = this.list(queryWrapper);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("list", list);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Result addDockServer(EntityDockServer entity) {
        EntityDockServer entityDockServer = getDockServerInfo(entity.getDockCode(), null, entity.getServerCode(), null);
        if (null != entityDockServer) {
            return ResultUtil.error(ResultEnum.Err_4093);
        }
        entityDockServer = getDockServerInfo(entity.getDockCode(), null, null, entity.getServerName());
        if (null != entityDockServer) {
            return ResultUtil.error(ResultEnum.Err_4094);
        }
        entityDockServer = getDockServerInfo(entity.getDockCode(), entity.getSvrAppid(), null, null);
        if (null != entityDockServer) {
            return ResultUtil.error(ResultEnum.Err_4095);
        }

        return this.save(entity) ? ResultUtil.success() : ResultUtil.error(ResultEnum.Err_2016);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Result updateDockServer(EntityDockServer entity) {
        EntityDockServer entityDockServer = getDockServerInfo(entity.getDockCode(), null, entity.getServerCode(), null);
        if (null == entityDockServer) {
            return ResultUtil.error(ResultEnum.Err_4096);
        }
        EntityDockServer entityResult = getDockServerByNotServerCode(entity.getDockCode(), entity.getServerCode(), entity.getServerName(), null);
        if (null != entityResult) {
            return ResultUtil.error(ResultEnum.Err_4094);
        }
        entityResult = getDockServerByNotServerCode(entity.getDockCode(), entity.getServerCode(), null, entity.getSvrAppid());
        if (null != entityResult) {
            return ResultUtil.error(ResultEnum.Err_4095);
        }
        entityDockServer.setServerName(entity.getServerName());
        entityDockServer.setSvrAppid(entity.getSvrAppid());
        entityDockServer.setRemark(entity.getRemark());
        return this.updateById(entityDockServer) ? ResultUtil.success() : ResultUtil.error(ResultEnum.Err_2016);

    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public EntityDockServer getDockServerByNotServerCode(String dockCode, String serverCode, String serverName, String svrAppid) {
        QueryWrapper<EntityDockServer> queryWrapper = Wrappers.query();
        if (StringUtils.isNotBlank(dockCode)) {
            queryWrapper.eq("dock_code", dockCode);
        }
        if (StringUtils.isNotBlank(serverCode)) {
            queryWrapper.ne("server_code", serverCode);
        }
        if (StringUtils.isNotBlank(serverName)) {
            queryWrapper.eq("server_name", serverName);
        }
        if (StringUtils.isNotBlank(svrAppid)) {
            queryWrapper.eq("svr_appid", svrAppid);
        }
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
}
